package org.dftproject.genesis.ui.agents.figures;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dftproject.genesis.core.agents.AgentManager;
import org.dftproject.genesis.core.agents.IAgent;
import org.dftproject.genesis.ui.agents.Activator;
import org.dftproject.genesis.ui.figures.CloseButtonFigure;
import org.dftproject.genesis.ui.figures.HyperlinkFigure;
import org.dftproject.genesis.ui.figures.IHyperlinkListener;
import org.dftproject.genesis.ui.figures.ISelectionListener;
import org.dftproject.genesis.ui.figures.TooltipLabel;
import org.dftproject.genesis.ui.figures.UniformSizeContainer;
import org.dftproject.genesis.ui.figures.UniformSizeGroup;
import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.ImageFigure;
import org.eclipse.draw2d.MarginBorder;
import org.eclipse.draw2d.ToolbarLayout;
import org.eclipse.swt.graphics.Color;

public class AgentFigure extends Figure {

	private static final Log log = LogFactory.getLog(AgentFigure.class);
	
	private final Color gradientTop = ColorConstants.white;
	private final Color gradientBottom = new Color(null, 234, 244, 255);
	private static final Color metadataColor = new Color(null, 0xAA, 0xAA, 0xAA); 

	private final IAgent agent;
	
	public AgentFigure(final IAgent agent, UniformSizeGroup linkGroup) {
		this.agent = agent;
		
		setBorder(new MarginBorder(3, 5, 3, 5));
		
		ToolbarLayout layout = new ToolbarLayout(true);
		layout.setStretchMinorAxis(true);
		layout.setSpacing(5);
		setLayoutManager(layout);
		
		ImageFigure icon = new ImageFigure(Activator.getImageDescriptor("icons/cog.png").createImage());
		add(icon);
		
		Figure container = new Figure();
		layout = new ToolbarLayout(false);
		layout.setStretchMinorAxis(true);
		container.setLayoutManager(layout);
		add(container);

		container.add(createHeader(linkGroup));
	}
	
	protected IFigure createHeader(UniformSizeGroup linkGroup) {
		Figure container = new Figure();
		ToolbarLayout layout = new ToolbarLayout(true);
		layout.setStretchMinorAxis(true);
		layout.setSpacing(30);
		container.setLayoutManager(layout);

		HyperlinkFigure link = new HyperlinkFigure(agent.getName());
		link.setAlwaysUnderline(false);
		link.addHyperlinkListener(new IHyperlinkListener() {

			public void linkActivated(Object data, int stateMask) {
				//
			}
			
		});
		container.add(new UniformSizeContainer(link, linkGroup, false));
		
		CloseButtonFigure closeButton = new CloseButtonFigure();
		closeButton.setFocusTraversable(true);
		closeButton.setToolTip(new TooltipLabel("Delete this agent"));
		closeButton.addSelectionListener(new ISelectionListener() {

			public void selected() {
				AgentManager.getDefault().removeAgent(agent);
			}
			
		});
		container.add(closeButton);
		
		return container;
	}
	
	@Override
	public void paintFigure(Graphics graphics) {
		super.paintFigure(graphics);
		
		graphics.setForegroundColor(gradientTop);
		graphics.setBackgroundColor(gradientBottom);
		graphics.fillGradient(getBounds(), true);
	}

}
